package com.wwlwxg.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.StreamEntry;
import redis.clients.jedis.StreamEntryID;

import java.util.List;
import java.util.Map;

public class StreamQuery{

	static Jedis jedis = new Jedis("localhost", 6379);

	public static void main(String[] args) throws Exception{String streamID = "MyStream";

		StreamEntryID start = new StreamEntryID(0,0);
		StreamEntryID end = null; // null -> until the last item in the stream
		int count = 2;
		try {
			List<StreamEntry> stList = jedis.xrange(streamID, start, end, count);
			if(stList != null) {
	// Process data here
				for(int j=0; j<stList.size(); j++) {
					StreamEntry streamData = (StreamEntry)stList.get(j); System.out.println(streamData); // Read the fields (key-value pairs) of data stream
					Map<String, String> fields = streamData.getFields(); // Read subsequent data from last id + 1
					StreamEntryID nextStart =
							new StreamEntryID(streamData.getID().getTime(),
									(streamData.getID().getSequence()+1));
				}
			}else {
				System.out.println("No new data in the stream");
			}
		}finally {
			jedis.close();
		}
}
}